{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob, re, os\n",
    "import logging\n",
    "import fairing\n",
    "GCP_PROJECT = fairing.cloud.gcp.guess_project_name()\n",
    "DOCKER_REGISTRY = 'gcr.io/{}'.format(GCP_PROJECT) # every Google Cloud Platform project comes with a private Docker registry\n",
    "base_image = \"{}/{}\".format(DOCKER_REGISTRY, \"fairing:latest\")\n",
    "logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR) # suppress nagging bug about a library incompatibility\n",
    "print(base_image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Authenticate with the docker registry first\n",
    "\n",
    "```bash\n",
    "gcloud auth configure-docker```\n",
    "\n",
    "If using TPUs please also authorize Cloud TPU to access your project as [described here](https://cloud.google.com/ml-engine/docs/tensorflow/using-tpus#authorize_your_to_access_your_project)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up your output bucket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BUCKET = \"gs://\"  # your bucket here\n",
    "assert re.search(r'gs://.+', BUCKET), 'A GCS bucket is required to store your results.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Build a base image to work with fairing\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# The base image for AI Platform Notebook instances\n",
      "FROM gcr.io/deeplearning-platform-release/tf-gpu.1-13\n",
      "# Stuff that should have been configured in the base image (bug)\n",
      "ENV LC_ALL C.UTF-8\n",
      "ENV LANG C.UTF-8\n",
      "# Upgrading to Tensorflow 2.0\n",
      "RUN pip install tensorflow-gpu==2.0.0-beta1"
     ]
    }
   ],
   "source": [
    "!cat Dockerfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!docker build . -t {base_image}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!docker push {base_image}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start an AI Platform job"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "additional_files = '' # If your code requires additional files, you can specify them here (or include everything in the current folder with glob.glob('./**', recursive=True))\n",
    "# If your code does not require any dependencies or config changes, you can directly start from an official Tensorflow docker image\n",
    "#fairing.config.set_builder('docker', registry=DOCKER_REGISTRY, base_image='gcr.io/deeplearning-platform-release/tf-gpu.1-13')\n",
    "\n",
    "# base image\n",
    "fairing.config.set_builder('docker', registry=DOCKER_REGISTRY, base_image=base_image)\n",
    "# AI Platform job hardware config\n",
    "fairing.config.set_deployer('gcp', job_config={'trainingInput': {'scaleTier': 'CUSTOM', 'masterType': 'standard_p100'}})\n",
    "# input and output notebooks\n",
    "fairing.config.set_preprocessor('full_notebook',\n",
    "                                notebook_file=\"05K_MNIST_TF20Keras_Tensorboard_playground.ipynb\",\n",
    "                                input_files=additional_files,\n",
    "                                output_file=os.path.join(BUCKET, 'fairing-output', 'mnist-001.ipynb'))\n",
    "\n",
    "\n",
    "# GPU settings for single K80, single p100 respectively\n",
    "# job_config={'trainingInput': {'scaleTier': 'BASIC_GPU'}}\n",
    "# job_config={'trainingInput': {'scaleTier': 'CUSTOM', 'masterType': 'standard_p100'}}\n",
    "\n",
    "# These job_config settings for TPUv2\n",
    "#job_config={'trainingInput': {'scaleTier': 'BASIC_GPU'}}\n",
    "#job_config={'trainingInput': {'scaleTier': 'CUSTOM', 'masterType': 'n1-standard-8', 'workerType': 'cloud_tpu', 'workerCount': 1,\n",
    "#                                               'workerConfig': {'accelerator_config': {'type': 'TPU_V2','count': 8}}}})\n",
    "# On AI Platform, TPUv3 support is alpha and available to whitelisted customers only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fairing.config.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## License"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "author: Martin Gorner<br>\n",
    "twitter: @martin_gorner\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "Copyright 2019 Google LLC\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "This is not an official Google product but sample code provided for an educational purpose\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
